package operations

import (
	"fmt"

	"gitee.com/knowgo/gpa-examples/commands"
	_ "gitee.com/knowgo/gpa-examples/db/repos/postgres"
	"gitee.com/knowgo/gpa-examples/examples/operations/class"
	"gitee.com/knowgo/gpa-examples/examples/utils"
	"gitee.com/knowgo/gpa/rt"
)

type oper struct {
	Flag string
	Desc string
	Func func(p *rt.Provider, args []string)
}

var opers = map[string]*oper{
	"add-class": {
		Desc: "添加班级",
		Func: class.AddClass,
	},
}

var Cmd = &commands.Command{
	Cmd:         "op",
	Describtion: "操作数据库",
	EnterPoint: func(appName string, args []string) {
		if len(args) <= 2 {
			printUsages(appName)
			return
		}
		o, ok := opers[args[2]]
		if !ok {
			fmt.Printf("Error: flag [%s] is not find\n", args[2])
			printUsages(appName)
		}
		p := utils.GetProvider()
		o.Func(p, args)
	},
}

func printUsages(appName string) {
	fmt.Println("Usage:")
	fmt.Printf("    %s op [flag]\n", appName)
	fmt.Println()
	fmt.Println("flags:")
	for flag, o := range opers {
		fmt.Printf("    %-10s %s\n", flag, o.Desc)
	}
}
